from dash import html, dcc



from server import server


from modules.bigscreen.callback2 import get_org_products_summary

from .charts import *

from .charts3 import *


# 获取所有机构名称
def get_all_org_names():
    with server.app_context():
        orgs = db.session.query(Organization.short_name_c).all()
        return [org[0] for org in orgs] if orgs else []


#创建设备金额数量柱状图
def create_org_equipment_amount_panel():
    
    return html.Div([
        html.Div(
            html.Span("资产分布统计", id="title-org-equipment-chart", n_clicks=0, style={ "textDecoration": "underline" ,"cursor": "pointer", "color": "#fff"}),
            className="panel-title",
            style={"color": "#112360", "fontSize": "16px","fontWeight": "bold",  "padding": "5px 5px"}
        ),
        dcc.Graph(
            id="org-equipment-chart",
            figure=create_org_equipment_amount_chart(*get_org_products_summary()),
            config={'displayModeBar': False},
            style={"height": "100%", "marginTop": "5px", "width": "90%", "margin": "0 auto"}
        )
    ], className="dashboard-panel", style={
        "margin": "2px 2px 2px 2px",
        
        "background": "#112360",
        "borderRadius": "10px",
        "boxShadow": "0 0 14px #1a4a7a",
        "flex": "1 1 0",
        "minHeight": "0",
        "display": "flex",
        "flexDirection": "column"
    })

#创建设备分类金额圆环图
def create_donut_panel():
    return html.Div([
        html.Div([
            html.Span("资产分类金额统计", id="title-donut-panel", n_clicks=0, style={ "textDecoration": "underline" ,"cursor": "pointer","background": "#112360", "color": "#ffffff", "fontSize": "16px", "fontWeight": "bold", "marginRight": "auto"}),
            dcc.Dropdown(
                id='donut-type-mode',
                options=[
                    {'label': '按金额', 'value': 'amount'},
                    {'label': '按台数', 'value': 'count'}
                ],
                value='amount',
                clearable=False,
                style={'width': '120px', 'marginLeft': '10px'}
            ),
            dcc.Dropdown(
                id='donut-org-selector',
                options=[{'label': '全部机构', 'value': 'all'}] + [
                    {'label': name, 'value': name} for name in get_all_org_names()
                ],
                value='all',
                clearable=False,
                style={'width': '200px', 'marginLeft': 'auto'}
            ),
        ], style={'display': 'flex', 'flexDirection': 'row', 'alignItems': 'center', 'justifyContent': 'space-between', 'marginBottom': '10px'}),
        html.Div([
            dcc.Graph(id='donut-type', config={'displayModeBar': False}, style={'width': '45%', 'display': 'inline-block', 'height': '201px','marginTop': '30px'}),
            dcc.Graph(id='donut-price', config={'displayModeBar': False}, style={'width': '45%', 'display': 'inline-block', 'height': '201px','marginTop': '30px'}),
        ], style={'display': 'flex', 'justifyContent': 'space-between'})
    ], className="dashboard-panel", style={
        "margin": "2px 2px 2px 2px",
        "background": "#112360",
        "borderRadius": "10px",
        "boxShadow": "0 0 16px #1a4a7a",
        "flex": "1 1 0",
        "minHeight": "0",
        "display": "flex",
        
        "flexDirection": "column"
    })

#创建完好率图表
def create_availability_panel():
    return html.Div([
        html.Div("设备完好率及年限指数", id="title-org-equipment-health-chart", n_clicks=0,className="panel-title", style={ "textDecoration": "underline" ,"cursor": "pointer","color": "#ffffff", "fontSize": "16px", "fontWeight": "bold", "padding": "5px 5px"}),
        dcc.Graph(
            id="org-equipment-health-chart",
            figure=create_org_equipment_health_chart(get_org_running_status_list()),
            config={'displayModeBar': False},
            style={"height": "100%", "marginTop": "5px", "width": "90%", "margin": "0 auto"}
        ),
    ], className="dashboard-panel", style={
        "margin": "2px 2px 2px 2px",
        "background": "#112360",
        "borderRadius": "10px",
        "boxShadow": "0 0 16px #1a4a7a",
        "flex": "1 1 0",
        "minHeight": "0",
        "display": "flex",
        "flexDirection": "column"
    })


# # #创建维修次数图表
def create_repair_count_panel():
    return html.Div([
        html.Div("维修次数统计",  id="title-org-equipment-repaire-count-chart", n_clicks=0,className="panel-title", style={ "textDecoration": "underline" ,"cursor": "pointer","color": "#ffffff", "background": "#112360","fontSize": "16px", "fontWeight": "bold", "padding": "5px 10px"}),      
        dcc.Graph(
            id="org-equipment-repaire-count-chart",
            figure=create_repair_count_chart(),
            config={'displayModeBar': False},
             style={"height": "100%", "marginTop": "5px", "width": "90%", "margin": "0 auto"}
        ),
    ], className="dashboard-panel", style={
        "margin": "2px 2px 2px 2px",
        "background": "#112360",
        "borderRadius": "10px",
        "boxShadow": "0 0 16px #1a4a7a",
        "flex": "1 1 0",
        "minHeight": "0",
        "display": "flex",
        "flexDirection": "column"
    })

#创建保养次数图表
def create_maintain_count_panel():
    return html.Div([
        html.Div("保养次数统计",id="title-org-equipment-maintain-count-chart", n_clicks=0, className="panel-title", style={ "textDecoration": "underline" ,"cursor": "pointer","color": "#ffffff","background": "#112360", "fontSize": "16px","fontWeight": "bold",  "padding": "5px 10px"}),      
        dcc.Graph(
            id="org-equipment-maintain-count-chart",
            figure=create_maintain_count_chart(),
            config={'displayModeBar': False},
            style={"height": "100%", "marginTop": "5px", "width": "90%", "margin": "0 auto"}
        ),
    ], className="dashboard-panel", style={
        "margin": "2px 2px 2px 2px",
        "background": "#112360",
        "borderRadius": "10px",
        "boxShadow": "0 0 16px #1a4a7a",
        "flex": "1 1 0",
        "minHeight": "0",
        "display": "flex",
        "flexDirection": "column"
    })

#创建巡检次数图表
def create_inspection_count_panel():
    return html.Div([
        html.Div("巡检次数统计",id="title-org-equipment-inspection-count-chart", n_clicks=0, className="panel-title", style={ "textDecoration": "underline" ,"cursor": "pointer","color": "#ffffff", "background": "#112360","fontSize": "16px","fontWeight": "bold",  "padding": "5px 10px"}),      
        dcc.Graph(
            id="org-equipment-inspection-count-chart",
            figure=create_inspection_count_chart(),
            config={'displayModeBar': False},
            style={"height": "100%", "marginTop": "5px", "width": "90%", "margin": "0 auto"}
        ),
    ], className="dashboard-panel", style={
        "margin": "2px 2px 2px 2px",
        "background": "#112360",
        "borderRadius": "10px",
        "boxShadow": "0 0 16px #1a4a7a",
        "flex": "1 1 0",
        "minHeight": "0",
        "display": "flex",
        "flexDirection": "column"
    })


def create_donut_big_modal(donut_mode='amount', donut_org='all'):
    return html.Div(
        id='donut-big-modal',
        style={
            'display': 'none',  # 初始隐藏，回调控制显示
            'position': 'fixed',
            'top': 0,
            'left': 0,
            'width': '100vw',
            'height': '100vh',
            'backgroundColor': 'rgba(0,0,0,0.5)',
            'zIndex': 9999,
            'justifyContent': 'center',
            'alignItems': 'center'
        },
        children=[
            html.Div([
                html.Div([
                    dcc.Dropdown(
                        id='big-donut-type-mode',
                        options=[
                            {'label': '按金额', 'value': 'amount'},
                            {'label': '按台数', 'value': 'count'}
                        ],
                        value=donut_mode,
                        clearable=False,
                        style={'width': '120px', 'marginLeft': '10px'}
                    ),
                    dcc.Dropdown(
                        id='big-donut-org-selector',
                        options=[{'label': '全部机构', 'value': 'all'}] + [
                            {'label': name, 'value': name} for name in get_all_org_names()
                        ],
                        value=donut_org,
                        clearable=False,
                        style={'width': '200px', 'marginLeft': '10px'}
                    ),
                    html.Button('关闭', id='close-donut-big-modal', style={'marginLeft': 'auto'})
                ], style={'display': 'flex', 'flexDirection': 'row', 'alignItems': 'center', 'justifyContent': 'flex-start', 'marginBottom': '10px'}),
                html.Div([
                    dcc.Graph(
                        id='big-donut-type',
                        figure=create_device_type_donut(org_name=donut_org, mode=donut_mode),
                        config={'displayModeBar': False},
                        style={'width': '48vw', 'display': 'inline-block', 'height': '500px'}
                    ),
                    dcc.Graph(
                        id='big-donut-price',
                        figure=create_device_price_donut(org_name=donut_org, mode=donut_mode),
                        config={'displayModeBar': False},
                        style={'width': '48vw', 'display': 'inline-block', 'height': '500px'}
                    ),
                ], style={'display': 'flex', 'justifyContent': 'space-between'})
            ], style={
                'background': '#112360',
                'borderRadius': '10px',
                'padding': '20px',
                'width': '90vw',
                'margin': '5vh auto',
                'boxShadow': '0 0 24px #1a4a7a'
            })
        ]
    )